home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / program / commio0b.zip / ASYNC.ASM < prev    next >
Assembly Source File  |  1994-11-21  |  25KB  |  819 lines

  1. ;    Status byte definition (C_Status):
  2.  
  3. ;    7   6   5   4   3   2   1   0
  4. ;    |   |   |   |   |   |   |   |____ Input buffer empty
  5. ;    |   |   |   |   |   |   |________ Input buffer full
  6. ;    |   |   |   |   |   |____________ Output buffer empty
  7. ;    |   |   |   |   |________________ Output buffer full
  8. ;    |   |   |   |____________________ Input buffer overflow
  9. ;    |   |   |________________________ Output buffer overflow
  10. ;    |   |____________________________ Hard handshake active (xmit stopped)
  11. ;    |________________________________ Soft handshake active (xmit stopped)
  12.  
  13. ;    Control byte definition (C_Ctrl):
  14.  
  15. ;    7   6   5   4   3   2   1   0
  16. ;    |   |   |   |   |   |   |   |____ Enable RTS handshake
  17. ;    |   |   |   |   |   |   |________ Enable CTS handshake
  18. ;    |   |   |   |   |   |____________ Enable software handshake
  19. ;    |   |   |   |   |________________
  20. ;    |   |   |   |____________________
  21. ;    |   |   |________________________
  22. ;    |   |____________________________
  23. ;    |________________________________
  24.  
  25. ;********************************************************
  26.  
  27. ; Macro:    PutChar
  28. ; Function:    Place character in transmit buffer (transmit a character)
  29. ; Entry:    AL <- Port # (0-3)
  30. ;        AH <- Status register
  31. ;        BX <- Byte array pointer
  32. ;        CH <- Character to put in buffer
  33. ;        DX <- Base address of port
  34. ; Destroyed:    ES,SI,DI,AL
  35.  
  36. ; Note:    Port address passed is NOT checked for validity.
  37. ;    Subroutine ChkPort will create the proper entry environment
  38.  
  39. PUTCHAR MACRO
  40.  
  41.         test    ah, 00001000b           ;Is buffer full?
  42.         JZ      @@putch1                ;No, OK to put character
  43.         or      ah, 00100000b           ;set overflow flag
  44.         JMP     @@putchx                ;and exit
  45.  
  46. @@putch1:
  47.         shl     bl,1                    ;bx <- word array index
  48.         mov     di, [C_outhead+bx]      ;bump the head pointer
  49.         mov     si, [c_outtail+bx]
  50.         inc     di
  51.         cmp     di, [C_outsize+bx]
  52.         jb      @@putch2
  53.         xor     di,di
  54. @@putch2:
  55.         mov     [C_outhead+bx],di       ;save head pointer
  56.         inc     [C_buffull+bx]          ;increment buffer-full word
  57.  
  58.         shl     bl,1                    ;BX <- pointer array index
  59.         LES     BX, [C_outbufptr+bx]    ;Load ES with segment of output buf
  60.         mov     [ES:BX+DI],ch           ;save the byte
  61.  
  62.         cmp     di, si                  ;Is output buffer full?
  63.         jnz     @@putch3                ;no, go enable xmitter
  64.         or      ah, 00001000b           ;Set status to output buffer full
  65. @@putch3:
  66.         xor     bh, bh
  67.         mov     bl, al                  ;Save AL in BL and make BX byte index
  68.  
  69.         test    ah, 11000000b           ;soft/hard handshake on?
  70.         jnz     @@putchx
  71. ;        test    ah, 00000100b
  72. ;        jz      @@putchx
  73.  
  74.         and     ah, 11111011b           ;Buffer is no longer empty
  75.         inc     dl                      ;Change to IER register
  76.         in      al, dx                  ;Read in the IER
  77.         or      al, 00000010b           ;Turn on xmt interrupt
  78.         NOP                             ;Slow down for slow machines
  79.         NOP
  80.         out     dx, al                  ;Write out the IER
  81.         dec     dl                      ;Back to base reg
  82. @@putchx:
  83.         mov     [C_status+bx],ah        ;set status byte
  84. ;        mov     al,bl                   ;get port num back in AL
  85.     ENDM
  86.  
  87. ;******************************************************************
  88. ; MACRO: Test for modem status change
  89. ;
  90. ; Entry: DX - port base
  91. ;        BX - byte array index
  92. ;
  93. ; Destroyed: AX, CX
  94.  
  95. MSCHG   MACRO
  96.  
  97. ;    Get modem status & control/status registers
  98.  
  99. @@Msc0:    ADD    DL,MSR            ;DX <- 8250 MSR port address
  100.     IN    AL,DX            ;AL <- 8250 Modem status
  101.     SUB    DL,MSR            ;Back to base
  102.  
  103.     MOV    CL,[C_Status+BX]    ;CL <- Status register
  104.     MOV    CH,[C_Ctrl+BX]        ;CH <- Control register
  105.  
  106. ;    Control CTS status flag based on CTS status and handshake enable
  107.  
  108.     TEST    CH,00000010b        ;CTS handshake enabled ?
  109.     JZ    @@Msc2            ; No, ignore CTS status
  110.     TEST    AL,00010000b        ;CTS asserted ?
  111.     JNZ    @@Msc1            ; Yes, enable transmit
  112.     OR    CL,01000000b        ;CTS inactive - hard handshake on
  113.     JMP    @@Msc2            ;
  114. @@Msc1:    AND    CL,10111111b        ;CTS active - hard handshake off
  115. @@Msc2:    MOV    [C_Status+BX],CL    ;Save status flags
  116.  
  117. ;    Determine if transmitter should be enabled
  118.  
  119. @@Msc3:    ADD    DL,IER            ;DX <- 8250 IER port address
  120.     IN    AL,DX            ;AL <- Interrupt enable register
  121.     OR    AL,00000010b        ;Enable transmitter by default
  122.     TEST    CL,11000100b        ;Enable transmitter ?
  123.     JZ    @@Msc4            ; Yes
  124.     AND    AL,11111101b        ; No - disable transmitter
  125. @@Msc4:    OUT    DX,AL            ;Update IER
  126.         SUB     DL, IER
  127.  
  128.         ENDM
  129.  
  130.     IDEAL
  131.  
  132.     SEGMENT    DATA     WORD PUBLIC
  133.  
  134. ;    Externally accessed (TP program) variables
  135. ;    Note:     Most of these variables are declared as arrays in the main
  136. ;        program; i.e. C_InSize : Array[1..4] Of Word
  137.  
  138.     EXTRN    C_InBufPtr :DWORD    ;Pointer to input buffers
  139.     EXTRN    C_OutBufPtr:DWORD    ;Pointer to output buffers
  140.     EXTRN    C_InSize   :WORD    ;Size (bytes) of input buffers
  141.     EXTRN    C_OutSize  :WORD    ;Size (bytes) of output buffers
  142.     EXTRN    C_InHead   :WORD    ;Input (receive) head pointers
  143.     EXTRN    C_OutHead  :WORD    ;Output (transmit) head pointers
  144.     EXTRN    C_InTail   :WORD    ;Input (receive) tail pointers
  145.     EXTRN    C_OutTail  :WORD    ;Output (transmit) tail pointers
  146.     EXTRN    C_RTSOn    :WORD    ;Point at which RTS line is asserted
  147.     EXTRN    C_RTSOff   :WORD    ;Point at which RTS line is dropped
  148.     EXTRN    C_StartChar:BYTE    ;Start character for soft handshake
  149.     EXTRN    C_StopChar :BYTE    ;Stop character for soft handshake
  150.     EXTRN    C_Status   :BYTE    ;Status byte (see above)
  151.     EXTRN    C_Ctrl     :BYTE    ;Control byte (see above)
  152.     EXTRN    C_PortOpen :BYTE    ;Port-open flags
  153.     EXTRN    C_PortAddr :WORD    ;Base address of ports
  154.     EXTRN    C_MaxCom   :BYTE    ;Highest port # defined (single byte)
  155.         EXTRN   C_bufFull  :WORD
  156.         EXTRN   C_cascade  :BYTE
  157. ;        EXTRN   C_CharSend  :WORD
  158. ;        EXTRN   C_CharWrite :WORD
  159. ;        EXTRN   C_Temp      :WORD    ;Used for debugging
  160.  
  161. ;    8250 register offsets
  162.  
  163. IER    EQU    1            ;Interrupt enable register
  164. IIR    EQU    2            ;Interrupt identification register
  165. LCR    EQU    3            ;Line control register
  166. MCR    EQU    4            ;Modem control register
  167. LSR    EQU    5            ;Line status register
  168. MSR    EQU    6            ;Modem status register
  169. SCR    EQU    7            ;8250 scratch register
  170.  
  171.     ENDS    DATA
  172.  
  173. ;    Code segment declaration
  174.  
  175.     SEGMENT    CODE    BYTE PUBLIC
  176.  
  177.     ASSUME    CS:CODE,DS:DATA
  178.  
  179. ;    Externally accessable procedures defined here
  180.  
  181.         PUBLIC  INT_Handler
  182.     PUBLIC    ComReadCh
  183.     PUBLIC    ComReadChW
  184.     PUBLIC    ComWriteCh
  185.     PUBLIC    ComWriteChW
  186.  
  187. ;********************************************************
  188. ;*                            *
  189. ;*    Subroutines that are used internally        *
  190. ;*                            *
  191. ;********************************************************
  192.  
  193. ; Subroutine:    ChkPort
  194. ; Function:    Check port parameter(s), ensure that port is OPEN
  195. ; Entry:    AL <- Port # (1 - C_MaxCom)
  196. ; Exit:        AL -> Adjusted port # (0 - 3)
  197. ;        AH -> Status register
  198. ;        BX -> Byte array index
  199. ;        DX -> Base address of port
  200. ;        Carry flag SET if parameters & port are OK
  201. ;
  202. ;    PROC    ChkPort        FAR
  203. ;
  204. ;    Determine if port # is valid
  205. ;
  206. ;    CMP    AL,[C_MaxCom]        ;Port # > Maximum port # ?
  207. ;    JA    ChkErr            ; Yes, exit w/error
  208. ;    CMP    AL,0            ;Port # = 0 (invalid port #)
  209. ;    JZ    ChkErr            ; Yes, exit w/error
  210. ;    DEC    AL            ;AL <- Adjusted port #
  211. ;
  212. ;    Check if port open
  213. ;
  214. ;    XOR    BH,BH            ;
  215. ;    MOV    BL,AL            ;BX <- Byte array index
  216. ;    MOV    AH,[C_PortOpen+BX]    ;AH <- Port-open flag
  217. ;    CMP    AH,0            ;Port open ?
  218. ;    JZ    ChkErr            ; No, exit w/error
  219. ;
  220. ;    Get status register and base port address in DX
  221. ;
  222. ;    MOV    AH,[C_Status+BX]    ;AH <- Status register
  223. ;    SHL    BL,1            ;BX <- Word array index
  224. ;    MOV    DX,[C_PortAddr+BX]    ;DX <- Port address
  225. ;    SHR    BL,1            ;BX <- Byte array index
  226. ;    STC                ;Set carry (valid return)
  227. ;    RET                ;Exit
  228. ;
  229. ;    Here if error
  230. ;
  231. ;ChkErr:    CLC                ;Clear carry (invalid return)
  232. ;    RET                ;Exit
  233. ;
  234. ;    ENDP    ChkPort
  235.  
  236. ;    PROC    PutChar        FAR
  237. ;
  238. ;;    Check for buffer overflow
  239. ;
  240. ;    TEST    AH,00001000b        ;Buffer full ?
  241. ;    JZ    PutCh1            ; No, continue
  242. ;    OR    AH,00100000b        ;Set buffer-overflow flag
  243. ;    JMP    PutChX            ;Exit
  244. ;
  245. ;;    Increment head pointer
  246. ;
  247. ;PutCh1:    SHL    BL,1            ;BX <- Word array index
  248. ;    MOV    DI,[C_OutHea